package main

import (
	"flag"
	"fmt"
	"zerorpc/app/gfzero/api/internal/config"
	"zerorpc/app/gfzero/api/internal/handler"
	"zerorpc/app/gfzero/api/internal/middleware"
	"zerorpc/app/gfzero/api/internal/svc"

	"github.com/zeromicro/go-zero/core/conf"
	"github.com/zeromicro/go-zero/rest"
)

var configFile = flag.String("f", "./etc/gfzeroapi.yaml", "the config file")

func main() {
	flag.Parse()

	var c config.Config
	conf.MustLoad(*configFile, &c)

	// 将全局配置保存在全局变量上
	config.GlobalConfig = c
	// 注册服务，传递服务配置
	server := rest.MustNewServer(c.RestConf)
	server.Use(middleware.WithRequestMiddleware)
	// 全局中间件  这儿主要用来捕获一下全局的异常，抛出给前端
	server.Use(middleware.WithExceptionMiddleware)
	defer server.Stop()

	// 为上下文赋值  将常用的方法绑定在上下文当中(redis、orm等等)
	ctx := svc.NewServiceContext(c)
	// 注册路由
	handler.RegisterHandlers(server, ctx)

	// 统一错误处理
	//httpx.SetErrorHandler(errors.ErrHandler)
	fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
	server.Start()
}
